﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace To4WebService
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    { 
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public void Login(string username,string password)
        {
            string json = JsonConvert.SerializeObject(new { Status = "failed" });
            if(UserDao.LoginCheck(username,password)>0)
            {

                json = JsonConvert.SerializeObject(UserDao.GetUserByUsername(username), Formatting.Indented,
                    new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeNonAscii });
            }

            //输出json数据
            //context:上下文
            //Response:响应
            Context.Response.Charset = "UTF-8";//传给浏览器的是utf8，网页，html标签的字符集
            Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");//页面内容字符集

            Context.Response.Write(json);
            Context.Response.End();
        }
    }
}
